/*
 * Copyright 2007 Dan Hodge
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.datajanitor.baja.spi;

import java.io.File;
import java.io.InputStream;

import net.datajanitor.baja.data.BoxFileInfo;
import net.datajanitor.baja.spi.data.AuthInfo;
import net.datajanitor.baja.spi.data.FileUploadResult;

/**
 * Provides a service-provider interface to the Box API. 
 */
public interface BoxConnectionProvider {
    /**
     * Creates a ticket which can be used to authenticate a user.
     * @return The ticket
     * @throws BoxSpiException
     */
    public String createTicket() throws BoxSpiException;
    
    /**
     * Authenticates the specified user.
     * @param ticket The ticket that was created by createTicket()
     * @param username Box account username
     * @param password password
     * @return An AuthInfo object for the user if the authentication is successful
     * @throws BoxSpiException If an error occurs during authentication
     */
    public AuthInfo authenticate(String ticket, String username, String password) throws BoxSpiException;
    
    /**
     * Uses the specified builder to build the complete file tree for the specified user. Does not 
     * download any files, just the information about all of the files in the specified user's account.
     * @param authInfo
     * @param builder
     * @throws BoxSpiException
     */
    public void getTree(AuthInfo authInfo, FileTreeBuilder builder) throws BoxSpiException;
    
    /**
     * Uploads a file to the user's account.
     * @param authInfo The AuthInfo object for an authenticated user
     * @param parent The parent directory that the file will be uploaded into
     * @param file The file being uploaded
     * @return The FileUploadResult representing the uploaded file, if successful
     * @throws BoxSpiException If an error occurs while uploading the file
     */
    public FileUploadResult uploadFile(AuthInfo authInfo, BoxFileInfo parent, File file) throws BoxSpiException;
    
    /**
     * Uploads a file to the user's account.
     * @param authInfo The AuthInfo object for an authenticated user
     * @param parent The parent directory that the file will be uploaded into
     * @param fileData The file data
     * @param fileName The name that the file will be stored under
     * @return The FileUploadResult representing the uploaded file, if successful
     * @throws BoxSpiException If an error occurs while uploading the file
     */
    public FileUploadResult uploadFile(AuthInfo authInfo, BoxFileInfo parent, byte[] fileData, String fileName) throws BoxSpiException;
    
    /**
     * Downloads and streams the specified file from the specified user's account. 
     * @param authInfo The AuthInfo object for an authenticated user
     * @param file The requested file
     * @return An InputStream that can be used to stream the file
     * @throws BoxSpiException If an error occurs while streaming the file.
     */
    public InputStream streamFile(AuthInfo authInfo, BoxFileInfo file) throws BoxSpiException;
    
    /**
     * Deletes the file or directory from the specified user's account.
     * @param authInfo The AuthInfo object for an authenticated user
     * @param file The file or directory that is being deleted
     * @return true If the file or directory was deleted successfully
     * @throws BoxSpiException In an error occurs while deleting the file.
     */
    public boolean delete(AuthInfo authInfo, BoxFileInfo file) throws BoxSpiException;
}
